package com.majun.huaxishe.controller;

import com.majun.huaxishe.entity.Photos;
import com.majun.huaxishe.result.Result;
import com.majun.huaxishe.utils.FtpUtils;
import com.majun.huaxishe.utils.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
@CrossOrigin
@Slf4j
public class UploadController {

    @Autowired
    private MinioUtils minioUtils;

//    /**
//     * 上面相册封面
//     */
//    @PostMapping("/cover")
//    public Result<String> uploadCover(MultipartFile file){
//        String url = minioUtils.upload2Minio(file,null);
//        return Result.success(url);
//    }

    /**
     * 上面相册
     */
//    @PostMapping("/photos")
//    public Result<Photos> uploadPhotos(MultipartFile file,String folderName){
//        String filename = file.getOriginalFilename();
//        int lastDotIndex = filename.lastIndexOf('.');
//        String photoName = filename.substring(0, lastDotIndex);
//        String url = minioUtils.upload2Minio(file,folderName);
//        Photos photos = new Photos();
//        photos.setName(photoName);
//        photos.setUrl(url);
//        return Result.success(photos);
//    }

//    @PostMapping("/background")
//    public Result<String> uploadBackground(MultipartFile file){
//        String url = minioUtils.upload2Minio(file,null);
//        return Result.success(url);
//    }

    @PostMapping("/cover")
    public Result<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
        String url = FtpUtils.upload2Ftp(file,null);
        return Result.success(url);
    }

    @PostMapping("/photos")
    public Result<Photos> uploadPhotos(MultipartFile file,String folderName){
        String filename = file.getOriginalFilename();
        int lastDotIndex = filename.lastIndexOf('.');
        String photoName = filename.substring(0, lastDotIndex);
        String url = FtpUtils.upload2Ftp(file,folderName);
        Photos photos = new Photos();
        photos.setName(photoName);
        photos.setUrl(url);
        return Result.success(photos);
    }

    @PostMapping("/background")
    public Result<String> uploadBackground(MultipartFile file){
        String url = FtpUtils.upload2Ftp(file,null);
        return Result.success(url);
    }
}
